package com.example.mannortycoon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mannortycoon.dao.TradeDao;
import com.example.mannortycoon.entity.CropEntity;
import com.example.mannortycoon.entity.TradeEntity;
import com.example.mannortycoon.entity.UserCropEntity;
import com.example.mannortycoon.entity.UserEntity;
import com.example.mannortycoon.service.CropService;
import com.example.mannortycoon.service.TradeService;
import com.example.mannortycoon.service.UserCropService;
import com.example.mannortycoon.service.UserService;
import com.example.mannortycoon.uitl.R;
import com.example.mannortycoon.vo.TradeVO;
import com.example.mannortycoon.vo.UserInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class TradeServiceImpl extends ServiceImpl<TradeDao,TradeEntity> implements TradeService {

    private final CropService cropService;

    private final UserService userService;

    private final UserCropService userCropService;

    @Autowired
    public TradeServiceImpl(CropService cropService,UserService userService,UserCropService userCropService){
        this.cropService = cropService;
        this.userService = userService;
        this.userCropService = userCropService;
    }

    @Override
    public List<TradeVO> listTrade(Long userId,Integer type) {
        QueryWrapper<TradeEntity> queryWrapper = new QueryWrapper<>();
        if(type == 0){
            queryWrapper.ne("user_id",userId);
        }else{
            queryWrapper.eq("user_id",userId);
        }
        List<TradeEntity> list = this.list();
        return list.stream().map(item->{
            TradeVO tradeVO = new TradeVO();
            BeanUtils.copyProperties(item,tradeVO);
            //查询用户信息
            UserInfo userInfo = userService.getUserInfo(item.getUserId());
            tradeVO.setUsername(userInfo.getUsername());
            //查询作物信息
            CropEntity byId = cropService.getById(item.getCropId());
            BeanUtils.copyProperties(byId,tradeVO);
            tradeVO.setTypeName(item.getSellType() == 0 ? "种子币" : "珍珠");
            return tradeVO;
        }).collect(Collectors.toList());
    }

    @Override
    @Transactional
    public R put(Long userId, TradeEntity tradeEntity) {
        //获取用户作物信息
        Long cropId = tradeEntity.getCropId();
        UserCropEntity userCropEntity = userCropService.getUserCropInfo(userId,cropId);
        if(userCropEntity == null || userCropEntity.getNumber() < tradeEntity.getNumber()) {
            return R.error("作物不足");
        }
        //判断价格是否符合范围
        //获取作物信息
        CropEntity cropEntity = cropService.getById(cropId);
        if(tradeEntity.getSellType() > cropEntity.getMaxSellPrice() ||
                tradeEntity.getSellType() < cropEntity.getMinSellPrice()){
            return R.error("价格不在允许范围内！");
        }
        //添加交易
        tradeEntity.setAvailabilityTime(new Date());
        this.save(tradeEntity);
        //减少用户作物
        userCropService.updateNumber(userId,cropId,-tradeEntity.getNumber());
        return R.ok();
    }

    @Override
    @Transactional
    public R trade(Long userId, Long id, Integer number) {
        //获取用户信息
        UserEntity userEntity = userService.getById(userId);
        //获取交易信息
        TradeEntity byId = this.getById(id);
        if(byId.getNumber() < number){
            return R.error("交易数量有误");
        }
        //判断售价种类
        if(byId.getSellType() == 0){
            if(byId.getSellType() * number > userEntity.getSeedGold()){
                return R.error("种子币不足");
            }
            //修改用户种子币
            userEntity.setSeedGold(userEntity.getSeedGold() - byId.getSellType() * number);
        }else{
            if(byId.getSellType() * number > userEntity.getPearl()){
                return R.error("珍珠不足");
            }
            //修改用户珍珠
            userEntity.setPearl(userEntity.getPearl() - byId.getSellType() * number);
        }
        //添加用户作物
        userCropService.updateNumber(userId,byId.getCropId(),number);
        //修改用户货币
        userService.updateById(userEntity);
        //减少交易
        if(byId.getNumber().equals(number)){
            //删除交易
            this.removeById(byId.getId());
        }else{
            byId.setNumber(byId.getNumber() - number);
            this.updateById(byId);
        }
        return R.ok();
    }
}
